/*
	Copyright (C) 2010 2011 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <QGraphicsScene>
#include <QGraphicsSceneHoverEvent>
#include <QPainter>
#include <QPen>
#include <QRect>

#include "ganttatask.h"
#include "ganttaxis.h"
#include "ganttscene.h"
#include "procycle.h"


GanttATask::GanttATask(ProCycle* app, PC::AtomTask* task, GanttCycle* gcycle, GanttMTask* parent) :
	GanttTask(app, task, gcycle, parent)
{
}


GanttATask::~GanttATask() {

}


QRectF GanttATask::boundingRect() const {
	return QRectF(
			0,
			0,
			scene()->width(),
			app()->rowHeight()
	);
}

void GanttATask::paintTaskBar(QPainter *painter) {
	painter->save();

	bool cp = m_drawCriticalPath && m_task->isCriticalPath();

	QColor c = GanttScene::colorForTask(m_task);
	painter->fillRect(m_rect, c);

	if (cp) {
		QRect cpr (m_rect);
		cpr.translate(0, 5);
		cpr.setHeight(m_rect.height()-7);
		painter->fillRect(cpr, Qt::red);
	}

	QPen pen (cp ? QColor(Qt::red) : GanttScene::borderColor(c));
	pen.setWidth((hovered()||isSelected()) ? 2 : 1);
	painter->setPen(pen);
	painter->drawRect(m_rect);

	painter->restore();
}



void GanttATask::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
	GanttTask::hoverMoveEvent(event);
	if (g_editMode == EDIT_TASK) {
		if (pointGrabsLength(event->pos())) {
			if (g_taskEditMode != TASKEDIT_LENGTH) {
				g_taskEditMode = TASKEDIT_LENGTH;
				setCursor(Qt::SizeHorCursor);
			}
		}
		else if (g_taskEditMode == TASKEDIT_LENGTH) {
			g_taskEditMode = TASKEDIT_NONE;
			setCursor(Qt::ArrowCursor);
		}
	}
}


void GanttATask::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
	GanttTask::hoverLeaveEvent(event);
	if (g_editMode == EDIT_TASK) {
		g_taskEditMode = TASKEDIT_NONE;
		setCursor(Qt::ArrowCursor);
	}
}


void GanttATask::mousePressEvent(QGraphicsSceneMouseEvent *event) {
	if (g_taskEditMode == TASKEDIT_LENGTH) {
		m_halfStep = app()->ganttAxis()->unit2px(1)/2;
		event->accept();
	}
	else {
		GanttTask::mousePressEvent(event);
	}
}

void GanttATask::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
	if (g_taskEditMode == TASKEDIT_LENGTH) {
		int length = app()->ganttAxis()->px2unit(event->pos().x()+m_halfStep - m_rect.left());
		if (length != m_task->length() && length>=0) {
			atask()->setLength(length);
		}
	}
	else {
		GanttTask::mouseMoveEvent(event);
	}
}

void GanttATask::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
	if (g_taskEditMode == TASKEDIT_LENGTH) {
		setCursor(Qt::ArrowCursor);
		g_taskEditMode = TASKEDIT_NONE;
	}
	else {
		GanttTask::mouseReleaseEvent(event);
	}
}


bool GanttATask::pointOnTaskBar(const QPointF &p) {
	return (p.x() <= m_rect.right()+1 && p.x() >= m_rect.left()-1 &&
			p.y() >= m_rect.top()-1 && p.y() <= m_rect.bottom()+3);
}

bool GanttATask::pointGrabsLength(const QPointF &p) {
	return (p.x() <= m_rect.right()+3 && p.x() >= m_rect.right()-3 &&
			p.y() >= m_rect.top()-1 && p.y() <= m_rect.bottom()+3);
}
